#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
#define getlow(x,k)  ( (x>>k)&1 )
using namespace std;
const int maxn=1e6+100;
typedef int LL;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[10][maxn];
LL dp[10][maxn];
bool ok2(LL x,LL y){
     if(getlow(x,0)^getlow(y,0)^getlow(x,1)^getlow(y,1)==0) return 0;
     else return 1;
}
bool ok3(LL x,LL y){
     if(getlow(x,0)^getlow(y,0)^getlow(x,1)^getlow(y,1)==0) return 0;
     if(getlow(x,1)^getlow(y,1)^getlow(x,2)^getlow(y,2)==0) return 0;
     else return 1;
}
LL cal2(LL id,LL y){
     LL sum=0;
     if(a[1][id]!=getlow(y,0)) sum++;
     if(a[2][id]!=getlow(y,1)) sum++;
     return sum;
}
LL cal3(LL id,LL y){
     LL sum=0;
     if(a[1][id]!=getlow(y,0)) sum++;
     if(a[2][id]!=getlow(y,1)) sum++;
     if(a[3][id]!=getlow(y,2)) sum++;
     return sum;
}
int main(void){
   cin.tie(0);std::ios::sync_with_stdio(false);
   LL n,m;cin>>n>>m;
   if(n>=4&&m>=4){
      cout<<"-1"<<"\n";return 0;
   }
   for(LL i=1;i<=n;i++){
       for(LL j=1;j<=m;j++) {
            char c;cin>>c;a[i][j]=c-'0';
       }
   }
   if(n==1){
      cout<<"0"<<"\n";
   }
   else if(n==2){
      memset(dp,0x3f,sizeof(dp));
      for(LL j=0;j<4;j++)  dp[j][1]=cal2(1,j);
      for(LL i=2;i<=m;i++){
          for(LL j=0;j<4;j++){
              for(LL k=0;k<4;k++){
                  if(!ok2(j,k)) continue;
                  dp[k][i]=min(dp[k][i],dp[j][i-1]+cal2(i,k));
              }
          }
      }
      LL ans=0x3f3f3f3f;
      for(LL j=0;j<4;j++){
          ans=min(ans,dp[j][m]);
      }cout<<ans<<"\n";
   }
   else if(n==3){
      memset(dp,0x3f,sizeof(dp));
      for(LL j=0;j<8;j++) dp[j][1]=cal3(1,j);
      for(LL i=2;i<=m;i++){
          for(LL j=0;j<8;j++){
              for(LL k=0;k<8;k++){
                  if(!ok3(j,k)) continue;
                  dp[k][i]=min(dp[k][i],dp[j][i-1]+cal3(i,k));
              }
          }
      }
      LL ans=0x3f3f3f3f;
      for(LL j=0;j<8;j++){
          ans=min(ans,dp[j][m]);
      }cout<<ans<<"\n";

   }
   return 0;
}
